package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.Banner;

public class GenericBannerDAO extends AutoKeys implements
		org.jetnuke.dao.BannerDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericBannerDAO.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.BannerModel#selectAllActiveOrderTypeBID()
	 */
	public List selectAllActiveOrderTypeBID() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAllActiveOrderTypeBID() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("BannerModel.selectAllActiveOrderTypeBID"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAllActiveOrderTypeBID() - end"); //$NON-NLS-1$
		}
		return list;
	}

	protected List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			Banner u = new Banner();
			fillBannerFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/**
	 * @param u
	 * @param rs
	 */
	private void fillBannerFromResultSet(Banner u, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("fillBannerFromResultSet(Banner, ResultSet) - start"); //$NON-NLS-1$
		}

		u.setACTIVE(rs.getInt("ACTIVE"));
		u.setALTTEXT(rs.getString("ALTTEXT"));
		u.setBID(rs.getInt("BID"));
		u.setCID(rs.getInt("CID"));
		u.setCLICKS(rs.getInt("CLICKS"));
		u.setCLICKURL(rs.getString("CLICKURL"));
		u.setDATE(rs.getTimestamp("DATE"));
		u.setDATEEND(rs.getTimestamp("DATEEND"));
		u.setIMAGEURL(rs.getString("IMAGEURL"));
		u.setIMPMADE(rs.getInt("IMPMADE"));
		u.setIMPTOTAL(rs.getInt("IMPTOTAL"));
		u.setTYPE(rs.getInt("TYPE"));

		if (logger.isDebugEnabled()) {
			logger.debug("fillBannerFromResultSet(Banner, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.BannerModel#selectAllInactiveOrderTypeBID()
	 */
	public List selectAllInactiveOrderTypeBID() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAllInactiveOrderTypeBID() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("BannerModel.selectAllInactiveOrderTypeBID"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAllInactiveOrderTypeBID() - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.BannerModel#deleteByID(int)
	 */
	public void deleteByID(int bid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("deleteByID(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("BannerModel.deleteByID"));
		p.setInt(1, bid);
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("deleteByID(int) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.BannerModel#retrieveByID(int)
	 */
	public Banner retrieveByID(int bid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByID(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("BannerModel.selectByID"));
		p.setInt(1, bid);

		ResultSet rs = p.executeQuery();
		Banner s = new Banner();

		if (rs.next()) {
			fillBannerFromResultSet(s, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByID(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.BannerModel#update(org.jetnuke.entities.Banner)
	 */
	public void update(Banner bn) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("update(Banner) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(SystemGlobals.getSql("BannerModel.update"));
		p.setInt(1, bn.getACTIVE());
		p.setString(2, bn.getALTTEXT());
		p.setInt(3, bn.getCID());
		p.setInt(4, bn.getCLICKS());
		p.setString(5, bn.getCLICKURL());
		p.setTimestamp(6, new Timestamp(bn.getDATE().getTime()));
		p.setTimestamp(7, null);// TODO CHECK!! new
								// Timestamp(bn.getDATEEND().getTime()));
		p.setString(8, bn.getIMAGEURL());
		p.setInt(9, bn.getIMPMADE());
		p.setInt(10, bn.getIMPTOTAL());
		p.setInt(11, bn.getTYPE());
		p.setInt(12, bn.getBID());
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("update(Banner) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.BannerModel#addNew(org.jetnuke.entities.Banner)
	 */
	public int addNew(Banner bn) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(Banner) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("BannerModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals
				.getSql("BannerModel.lastGeneratedBannerID"));
		initNewBanner(bn, p);

		int id = this.executeAutoKeysQuery(p);
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(Banner) - end"); //$NON-NLS-1$
		}
		return id;
	}

	/**
	 * @param bn
	 * @param p
	 */
	private void initNewBanner(Banner bn, PreparedStatement p) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("initNewBanner(Banner, PreparedStatement) - start"); //$NON-NLS-1$
		}

		p.setInt(1, bn.getACTIVE());
		p.setString(2, bn.getALTTEXT());
		p.setInt(3, bn.getCID());
		p.setInt(4, bn.getCLICKS());
		p.setString(5, bn.getCLICKURL());
		p.setTimestamp(6, new Timestamp(bn.getDATE().getTime()));
		p.setTimestamp(7, null);// TODO CHECK!! new
								// Timestamp(bn.getDATEEND().getTime()));
		p.setString(8, bn.getIMAGEURL());
		p.setInt(9, bn.getIMPMADE());
		p.setInt(10, bn.getIMPTOTAL());
		p.setInt(11, bn.getTYPE());

		if (logger.isDebugEnabled()) {
			logger.debug("initNewBanner(Banner, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.BannerModel#selectByCID(int)
	 */
	public List selectByCID(int cid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectByCID(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("BannerModel.selectByCID"));
		p.setInt(1, cid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectByCID(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.BannerModel#incClicks(int)
	 */
	public void incClicks(int bid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("incClicks(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(SystemGlobals.getSql("BannerModel.incClicks"));
		p.setInt(1, bid);
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("incClicks(int) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.BannerModel#countActiveByType(int)
	 */
	public int countActiveByType(int type) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("countActiveByType(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("BannerModel.countActiveByType"));
		p.setInt(1, type);
		ResultSet rs = p.executeQuery();
		rs.next();
		int r = rs.getInt(1);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("countActiveByType(int) - end"); //$NON-NLS-1$
		}
		return r;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.BannerModel#selectActiveByTypeOffset(int, int)
	 */
	public List selectActiveByTypeOffset(int type, int bannum) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectActiveByTypeOffset(int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("BannerModel.selectActiveByTypeOffset"));
		p.setInt(1, type);
		p.setInt(2, bannum);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectActiveByTypeOffset(int, int) - end"); //$NON-NLS-1$
		}
		return list;
	}

}
